<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-03 00:33:05
 * @LastEditTime: 2023-11-03 16:06:16
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: BOM成品工艺
 */

namespace App\Models;

use App\Traits\AttachmentTrait;
use App\Traits\AutoCodeTrait;
use App\Traits\StorageTrait;

class CustomerStorageDetail extends BaseModel
    {
    protected $appends = [];
    public function main()
        {
        return $this->belongsTo(CustomerStorage::class, "main_id");
        }
    use StorageTrait;
    use AutoCodeTrait;
    public $timestamps = true;
    public $table = "sys_customer_receive_details";
    protected $guarded = [];

    public function status()
        {
        return $this->belongsTo(Dic::class, "status_id", "key")->where(function ($q)
            {
            $q->where("code", 'status_with_finance');
            });
        }
    public function instorage_status()
        {
        return $this->belongsTo(Dic::class, "instorage_status_id", "key")->where(function ($q)
            {
            $q->where("code", 'InStorage');
            });
        }
    public function self_instorage_status()
        {
        return $this->belongsTo(Dic::class, "self_instorage_status_id", "key")->where(function ($q)
            {
            $q->where("code", 'InStorage');
            });
        }
    public function backing_category()
        {
        return $this->belongsTo(MaterialCategory::class);
        }
    public function washed_index()
        {
        return $this->belongsTo(Dic::class, "washed_index_id", "key")->where(function ($q)
            {
            $q->where("code", 'washed_index');
            });
        }
    public function strip_index()
        {
        return $this->belongsTo(Dic::class, "strip_index_id", "key")->where(function ($q)
            {
            $q->where("code", 'strip_index');
            });
        }
    public function backwarehouse()
        {
        return $this->hasMany(MarketBackwarehouseDetails::class, "batchno", "batchno");
        }
    public function end_inspect()
        {
        return $this->hasMany(ProProcessesInspect::class, "batchno", "batchno")->where("state", 0);
        }
    public function all_inspect()
        {
        return $this->hasMany(ProProcessesInspect::class, "batchno", "batchno");
        }
    public function updateQuantity()
        {
        $this->delivery_quantity = $this->hasMany(MarketDeliveryDetails::class, "batchno", "batchno")->sum("quantity");

        $wip_Data = $this->hasMany(ProProcessesWip::class, "batchno", "batchno")->get();

        $fh1_quantity       = $fh2_quantity = $fh3_quantity = $end_quantity = $dj_quantity = 0;
        $azijia             = [];
        $jh_quantity        = 0;
        $this->wip_quantity = $wip_Data->sum(function ($item) use (&$fh1_quantity, &$fh2_quantity, &$fh3_quantity, &$end_quantity, &$azijia, &$dj_quantity, &$jh_quantity)
            {
            $process_name = $item->open_card->processrename?->rename ?? "计划";
            $fh1_quantity += in_array($process_name, ['复合结束1', '复合结束', '复合开始1', '复合开始']) ? $item->faultle_quantity : 0;
            $fh2_quantity += in_array($process_name, ['复合结束2', '复合开始2']) ? $item->faultle_quantity : 0;
            $fh3_quantity += in_array($process_name, ['复合结束3', '复合开始3']) ? $item->faultle_quantity : 0;
            $end_quantity += in_array($process_name, ['生产结束', '保温结束']) ? $item->faultle_quantity : 0;
            $dj_quantity += in_array($process_name, ['倒卷']) ? $item->faultle_quantity : 0;
            $azijia[]    = $item->remark;
            $jh_quantity += $process_name == '计划' ? $item->faultle_quantity : 0;
            return $process_name != '计划' ? $item->faultle_quantity : 0;
            });
        //复合1复合2复合3生产结束合计，A字架
        $this->fh1_quantity = $fh1_quantity;
        $this->fh2_quantity = $fh2_quantity;
        $this->fh3_quantity = $fh3_quantity;
        $this->end_quantity = $end_quantity;
        $this->dj_quantity  = $dj_quantity;
        $this->azijia       = implode(",", $azijia);
        $this->th_quantity  = $this->backwarehouse()->sum("quantity");

        $first_inspect = $this->inspect;
        if ($first_inspect) {
            $this->reel           = $first_inspect->pack->count();
            $this->check_quantity = $first_inspect->check_quantity;
            $this->fect_quantity  = $first_inspect->quantity;
            }
        $end_inspect = $this->end_inspect;
        if ($end_inspect->count() >= 1) {
            $this->pro_reel            = $end_inspect->sum(function ($v)
                {
                return $v->pack->count();
                });
            $this->instorage_status_id = 1;
            //终检数
            $this->pro_quantity = $end_inspect->sum('quantity');
            }
        else {
            $this->instorage_status_id = 0;
            $this->pro_quantity        = 0;
            }
        $this->open_card->filter(function ($open_card_m)
            {
            //初检数 没有用开卡数
            $fect_quantity = $this->fect_quantity === 0 ? $open_card_m->quantity : $this->fect_quantity;

            //初检损耗率 （来货数-初检数）/来货数
            if ($this->check_quantity > 0) {
                $open_card_m->first_lost_rate = ($this->check_quantity - $fect_quantity) / $this->check_quantity;
                $open_card_m->save();
                }
            //成品损耗率 （初检数-成品数）/初检数
            if ($this->fect_quantity > 0 && ($open_card_m->inspect?->quantity ?? 0) > 0) {
                $open_card_m->end_lost_rate = ($this->fect_quantity - $open_card_m->inspect?->quantity ?? 0) / $this->fect_quantity;
                $open_card_m->save();
                }
            });
        //终检数 减去发货数 就是成品库存
        $this->storage_quantity = $this->pro_quantity - $this->delivery_quantity;
        $this->storage_quantity = $this->storage_quantity < 0 ? 0 : $this->storage_quantity;
        //实际库存=开卡-在制品+原料-退货
        $this->wip_storage_quantity = $jh_quantity + $this->lack_quantity - $this->th_quantity;
        $this->save();
        }
    public function updateRoll_quantity()
        {
        $this->roll_quantity = $this->open_card()->sum("roll_quantity");
        $this->save();
        }
    public function process()
        {
        return $this->belongsTo(ProProcesses::class);
        }
    public function route_process()
        {
        return $this->belongsTo(ProProcessRouteDetails::class);
        }

    public function craft_category()
        {
        return $this->belongsTo(Dic::class, "craft_category_id", "key")->where(function ($q)
            {
            $q->where("code", 'CraftCategory');
            });
        }

    public function film_category()
        {
        return $this->belongsTo(Dic::class, "film_category_id", "key")->where(function ($q)
            {
            $q->where("code", 'FilmCategory');
            });
        }

    public function open_card()
        {
        return $this->hasMany(OpenCardDetails::class, "batchno", "batchno");
        }
    public function pack()
        {
        return $this->hasMany(CustomerStorageInspectPack::class, "main_id");
        }
    public function scrap()
        {
        return $this->hasMany(CustomerStorageInspectPackScrap::class, "main_id");
        }

    public function inspect()
        {
        return $this->hasOne(CustomerStorageInspect::class, 'order_id');
        }
    public function finish()
        {
        return $this->belongsTo(Dic::class, "finish_id", "key")->where(function ($q)
            {
            $q->where("code", 'FinishStatus');
            });
        }
    public function customer_style()
        {
        return $this->hasMany(CustomerStorageStyle::class, "order_id");
        }

    public function inside_barcode()
    {
        return $this->belongsTo(Barcode::class, "inside_barcode_id");
    }

    public function outside_barcode()
    {
        return $this->belongsTo(Barcode::class, "outside_barcode_id");
    }

    public function cancel()
    {
        return $this->belongsTo(Dic::class, "is_cancel", "key")->where(function ($q) {
            $q->where("code", 'cancel_name');
        });
    }

    }

    